home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Celestin Apprentice 4
/
Apprentice-Release4.iso
/
Languages
/
RLaB 1.18c
/
testmatrix
/
trap2tri.r
< prev
next >
Wrap
Text File
|
1994-12-20
|
2KB
|
79 lines
//-------------------------------------------------------------------//
// Synopsis: Unitary reduction of trapezoidal matrix to triangular form.
// Syntax: T = trap2tri ( L )
// Description:
// Where L is an m-by-n lower trapezoidal matrix with m >= n,
// produces a unitary Q such that QL = [T; 0], where T is n-by-n
// and lower triangular. Q is a product of Householder
// transformations.
// Trap2tri returns a list with elements `q' and `t'.
// Reference:
// G.H. Golub and C.F. Van Loan, Matrix Computations, second edition,
// Johns Hopkins University Press, Baltimore, Maryland, 1989.
// P5.2.5, p. 220.
// This file is a translation of trap2tri.m from version 2.0 of
// "The Test Matrix Toolbox for Matlab", described in Numerical
// Analysis Report No. 237, December 1993, by N. J. Higham.
//-------------------------------------------------------------------//
trap2tri = function ( L )
{
local (L)
n = L.nr; r = L.nc;
if (r > n || norm(L-tril(L),"1")) {
error("Matrix must be lower trapezoidal and m-by-n with m >= n.");
}
Q = eye(n,n); // To hold product of H.T.s
if (r != n)
{
// Reduce nxr L = r [L1] to lower triangular form: QL = [T].
// n-r [L2] [0]
for (j in r:1:-1)
{
// x is the vector to be reduced, which we overwrite with the H.T. vector.
x = L[j:n;j];
x[2:r-j+1] = zeros(r-j,1); // These elts of column left unchanged.
s = norm(x,"2")*(sign(x[1]) + (x[1]==0)); // Modification for sign(1)=1.
// Nothing to do if x is zero (or x=a*e_1, but we don't check for that).
if (s != 0)
{
x[1] = x[1] + s;
beta = s'*x[1];
// Implicitly apply H.T. to pivot column.
// L(r+1:n,j) = zeros(n-r,1); // We throw these elts away at the end.
L[j;j] = -s;
// Apply H.T. to rest of matrix.
if (j > 1)
{
y = x'*L[j:n; 1:j-1];
L[j:n; 1:j-1] = L[j:n; 1:j-1] - x*(y/beta);
}
// Update H.T. product.
y = x'*Q[j:n;];
Q[j:n;] = Q[j:n;] - x*(y/beta);
}
}
}
T = L[1:r;]; // Rows r+1:n have been zeroed out.
return << q = Q; t = T >>;
};